<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- 1、导入mapbox的依赖 -->
    <script src="https://lib.baomitu.com/jquery/2.2.4/jquery.js"></script>
    <script src='https://api.mapbox.com/mapbox-gl-js/v2.15.0/mapbox-gl.js'></script>
    <link href='https://api.mapbox.com/mapbox-gl-js/v2.15.0/mapbox-gl.css' rel='stylesheet' />
    <style>
        * {
            margin: 0;
            padding: 0
        }

        #map {
            width: 100vw;
            height: 100vh;
        }

        #mouse-position {
            position: fixed;
            z-index: 100;
            bottom: 10px;
            left: 50%;
            width: 200px;
            height: 50px;
            background-color: #652e80;
            color: #fff;
            line-height: 50px;
            text-align: center;
        }
    </style>
</head>

<body>
    <div id="mouse-position"></div>
    <!-- 2、设置地图容器的挂载点 -->
    <div id="map">

    </div>
    <script>
        /* token */
        const gaode = {
            "version": 8,
            "sources": {
                "raster-tiles": {
                    "type": "raster",
                    "tiles": ["http://webrd01.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=8&x={x}&y={y}&z={z}"],
                    "tileSize": 256
                }
            },
            "layers": [{
                "id": "simple-tiles",
                "type": "raster",
                "source": "raster-tiles",
                "minzoom": 0,
                "maxzoom": 22
            }]
        }
        let mapbox_style = 'mapbox://styles/mapbox/streets-v12'
        mapboxgl.accessToken = 'pk.eyJ1IjoiZXJpYzA5MTciLCJhIjoiY2x3cTI2Zzg0MWY2dDJqcWVpdDI1MHNpcCJ9.k9gLW7ZgLyISNdYHIEEGdQ'
        const map = new mapboxgl.Map({
            /* target-ol */
            container: 'map', // container ID
            /* style-layers */
            style: mapbox_style, // style URL
            center: [114.30, 30.50], // starting position [lng, lat]
            zoom: 12, // starting zoom
        });
        map.on("style.load", () => {
            map.addControl(new mapboxgl.AttributionControl({
                //是否折叠属性信息
                compact: true,
                //自定义属性信息
                customAttribution: ["中地数码", "GIS高校论坛"]
            }));

            const scale = new mapboxgl.ScaleControl({
                unit: 'metric|imperial' //默认为公里
            })
            map.addControl(scale);
        })
        /* 通过属性事件,设置鼠标位置的经纬度到DOM
        mouse-position
          */
        map.on("mousemove", evt => {
            console.log(evt)
            var { lng, lat } = evt.lngLat;
            $("#mouse-position").html(
                `经度:${lng.toFixed(2)},纬度:${lat.toFixed(2)}`
            )
        })
    </script>
</body>

</html>